﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Login
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            Test t = new Test();
            SubTest st = new SubTest();

            InitializeComponent();
        }

        public string UserName
        {
            get
            {
                return System.AppDomain.CurrentDomain.GetData("username") == null ? "" : System.AppDomain.CurrentDomain.GetData("username").ToString();
            }
            private set
            {
                System.AppDomain.CurrentDomain.SetData("username", tbUser.Text);
            }
        }

        public string Password
        {
            get
            {
                return System.AppDomain.CurrentDomain.GetData("password") == null ? "" : System.AppDomain.CurrentDomain.GetData("password").ToString();
            }
            private set
            {
                System.AppDomain.CurrentDomain.SetData("password", tbPass.Text);
            }
        }

        public string GetUserName()
        {
            return tbUser.Text;
        }

        public string GetPassword()
        {
            return tbPass.Text;
        }

        private void FormLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            //DialogResult dr = MessageBox.Show("存在未保存的数据，是否继续关闭窗口", "警告！", MessageBoxButtons.OKCancel);
            //if (dr == DialogResult.Cancel)
            //{
            //    e.Cancel = true;
            //}
            UserName = tbUser.Text;
            Password = tbPass.Text;
            //System.AppDomain.CurrentDomain.SetData("username", tbUser.Text);
            //System.AppDomain.CurrentDomain.SetData("password", tbPass.Text);
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            btnLogin.Enabled = false;
            tbPass.Focus();
        }

        private void tbUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (tbUser.Text != "" && e.KeyCode == Keys.Enter)
            {
                tbPass.Focus();
            }
        }

        private void tbPass_KeyUp(object sender, KeyEventArgs e)
        {
            if (tbPass.Text != "" && e.KeyCode == Keys.Enter)
            {
                btnLogin.PerformClick();
            }
        }

        private void tbUser_TextChanged(object sender, EventArgs e)
        {
            btnLogin.Enabled = !string.IsNullOrWhiteSpace(tbUser.Text) && !string.IsNullOrWhiteSpace(tbPass.Text);

            //""  null   "  "
            //if (!string.IsNullOrWhiteSpace(tbUser.Text) && !string.IsNullOrWhiteSpace(tbPass.Text))
            //{
            //    btnLogin.Enabled = true;
            //}
            //else
            //{
            //    btnLogin.Enabled = false;
            //}
        }

        private void tbPass_TextChanged(object sender, EventArgs e)
        {
            btnLogin.Enabled = !string.IsNullOrWhiteSpace(tbUser.Text) && !string.IsNullOrWhiteSpace(tbPass.Text);
            //if (!string.IsNullOrWhiteSpace(tbUser.Text) && !string.IsNullOrWhiteSpace(tbPass.Text))
            //{
            //    btnLogin.Enabled = true;
            //}
            //else
            //{
            //    btnLogin.Enabled = false;
            //}
        }

        private void tbUserPass_TextChanged(object sender, EventArgs e)
        {
            btnLogin.Enabled = !string.IsNullOrWhiteSpace(tbUser.Text) && !string.IsNullOrWhiteSpace(tbPass.Text);
        }
    }


    class Test
    {
        private string strName1;
        protected string strName2;
        public string strName3;

        public Test()
        {
        }
    }

    class SubTest : Test
    {
        public SubTest()
        {
        }
    }

}
